﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace RandomGenerator
{
    class XMLGenerator
    {
        private XmlDocument doc;
        private const string PATH = @"C:\Documents and Settings\Chad\Desktop\test.xml";

        /*
         * Level # is always >= 1
         * GameType = "Continuous" or "Random"
         * GameMode = "Single" or "Multi"
         *
         * Example:
         *      LevelGenerator fileGenerator = new LevelGenerator();
         *      fileGenerator.CreateLevels(5, "Random", "Multi");
         * 
         * Do this with every New_Game
         */

        public void CreateLevels(int Level, string GameType, string GameMode)
        {
            switch (GameType)
            {
                // Each round continues from the round before it
                case "Continuous":
                    ContSequence(Level,GameMode);
                        break;
                // Each round is longer but a completely new sequence
                case "Random":
                    RandSequence(Level,GameMode);
                    break;
            }
        }
        public void ContSequence(int Level, string GameMode)
        {
            /*
             * Run a nested loop to convert the sequence to xml elements
             * Write to file
             */
            List<string> FullList = GenList(Level,GameMode);

            //Create an xml document
            doc = new XmlDocument();

            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            XmlElement root = doc.CreateElement("Levels");
            
            //Construct the document
            doc.AppendChild(declaration);
            doc.AppendChild(comment);

            for (int i = 0; i < Level; i++)
            {
                XmlElement level = doc.CreateElement("Level");
                XmlAttribute number = doc.CreateAttribute("Number");
                //Add the values for each nodes
                number.Value = Convert.ToString(i+1);
                level.Attributes.Append(number);

                for (int n = 0; n <= i; n++)
                {
                    XmlElement pattern = doc.CreateElement("Pattern");
                    XmlAttribute id = doc.CreateAttribute("Id");
                    id.Value = Convert.ToString(n+1);
                    pattern.Attributes.Append(id);
                    pattern.InnerText = FullList[n];
                    level.AppendChild(pattern);
                }

                root.AppendChild(level);
            }

            doc.AppendChild(root); 
            doc.Save(PATH);
        }

        public void RandSequence(int Level, string GameMode)
        {
            /*
             * Run a loop to get multiple sets of numbers
             * Convert each to xml elements
             * Write file
             */

            //Create an xml document
            doc = new XmlDocument();

            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            XmlElement root = doc.CreateElement("Levels");

            //Construct the document
            doc.AppendChild(declaration);
            doc.AppendChild(comment);

            for (int i = 0; i < Level; i++)
            {
                List<string> FullList = GenList(Level,GameMode);
                XmlElement level = doc.CreateElement("Level");
                XmlAttribute number = doc.CreateAttribute("Number");

                //Add the values for each nodes
                number.Value = Convert.ToString(i + 1);
                level.Attributes.Append(number);

                for (int n = 0; n <= i; n++)
                {
                    XmlElement pattern = doc.CreateElement("Pattern");
                    XmlAttribute id = doc.CreateAttribute("Id");
                    id.Value = Convert.ToString(n + 1);
                    pattern.Attributes.Append(id);
                    pattern.InnerText = FullList[n];
                    level.AppendChild(pattern);
                }

                root.AppendChild(level);
            }

            doc.AppendChild(root);
            doc.Save(PATH);
        }

    }
}
